<?php
namespace UploadFiles;
class UploadFilesView
{
	private $uploadFolder = 'plugin/UploadFiles/upload/';

	public function DoUploadFilesForm()
	{
		$html = '';

		$html .= '<form action="" method="POST" enctype="multipart/form-data">' . "\r\n";
		$html .= '    <fieldset name="Upload files">' . "\r\n";
		$html .= '    <legend>Ladda upp filer</legend>' . "\r\n";
		$html .= '        <label for="file">Välj fil:</label>' . "\r\n";
		$html .= '        <input type="file" name="file" id="file" />' . "\r\n";
		$html .= '        <input type="submit" name="upload_submit" value="Ladda upp" />' . "\r\n";
		$html .= '    </fieldset>' . "\r\n";
		$html .= '</form>' . "\r\n";

		return $html;
	}

	public function TriedToUploadFile()
	{
		return isset( $_POST['upload_submit'] ) ? $_POST['upload_submit'] : NULL;
	}

	public function ListDirectory( $directoryInformation, $pagePath )
	{
		$html = '';
		$html .= '<div class="folderListing">';
		$html .= '    <ul>';

		$operator = ( strlen( $_SERVER['QUERY_STRING'] ) > 0 ) ? '&' : '?';

		foreach( $directoryInformation as $info )
		{
			$html .= '    <li><a href="' . $_SERVER['REQUEST_URI'] . '' . $operator . 'dir=' . rawurlencode( $info ) . '">' . $info . '</a></li>';
		}
		$html .= '</ul>';
		$html .= '</div>';

		return $html;
	}

	public function ListDirectoryContent( $directoryContent, $pagePath )
	{
		$html = '';
		$html .= '<div class="folderListing">';
		$html .= '    <ul>';

		if ( $directoryContent != '' )
		{
			foreach( $directoryContent as $info )
			{
				$html .= '    <li><a href="' . $this->uploadFolder . $_GET['dir'] . '/' . $info . '">' . $info . '</a></li>';
			}
		}
		else
		{
			$html .= '<p>Tom mapp.</p>';
		}
		
		if ( strlen( $_SERVER['QUERY_STRING'] ) > 0 )
		{
			$html .= '<a href="' . $pagePath . '">Gå tillbaka</a>';
		}
		else
		{
			$html .= '<a href="/">Gå tillbaka</a>';
		}
		
		$html .= '</ul>';
		$html .= '</div>';

		return $html;
	}
}